<template>
  <div class="pages-article-classify">
    <page-cover :bg="classifyDetail.coverImg">
      <div class="title">{{ classifyDetail.name }}</div>
      <p>
        <span>文章 {{ covering(articleStatistics.articleNum) }}</span> ·
        <span>原创 {{ covering(articleStatistics.originalNum) }}</span>
      </p></page-cover
    >
    <main>
      <div class="list">
        <article-lists
          @paging="switchPaging"
          :lists="articleList"
          :loading="loading"
          :loadEnd="loadEnd"
        ></article-lists>
      </div>
    </main>
  </div>
</template>

<script>
import articleLists from "@/components/article-lists/article-lists";
import pageCover from "@/components/page-cover/page-cover";

export default {
  name: "WorkspaceJsonNote",
  components: {
    pageCover,
    articleLists,
  },
  data() {
    return {
      postData: {
        pageSize: 8,
        pageIndex: 1,
        classify: "",
      },
      loading: true,
      loadEnd: false,
      articleList: [],
      articleStatistics: {},
      classifyDetail: {},
    };
  },

  mounted() {
    this.postData.classify = this.$route.params.id;
    this.getArticleList();
    this.getArticleStatistics();
    this.getClassifyDetail();
  },

  methods: {
    getClassifyDetail() {
      this.$http("getClassifyDetail", {
        articleClassifyId: this.postData.classify,
      }).then((res) => {
        console.log(res, "res-res");
        this.classifyDetail = res.data;
      });
    },
    // 获取文章统计数据
    getArticleStatistics() {
      this.$http("getArticleStatistics", {
        classifyId: this.postData.classify,
      }).then((res) => {
        this.articleStatistics = res.data;
      });
    },
    // 获取文章列表
    getArticleList(type) {
      this.$http("getArticleList", this.postData).then((res) => {
        if (type == "page") {
          this.articleList = [...this.articleList, ...res.data.lists];
        } else {
          this.articleList = res.data.lists;
        }
        // 是否加载完毕
        if (res.data.lists.length < this.postData.pageSize) {
          this.loadEnd = true;
        }

        this.loading = false;
      });
    },
    covering(num = 0) {
      return num > 9 ? num : "0" + num;
    },
    switchPaging() {
      this.loading = true;
      this.postData.pageIndex += 1;
      this.getArticleList("page");
    },
  },
};
</script>

<style lang="scss" scoped>
.pages-article-classify {
  cursor: pointer;
}

.cover {
  .title {
    font-size: 30px;
    font-weight: 600;
  }
  p {
    font-size: 16px;
    opacity: 0.8;
    margin-top: 5px;
    span {
      margin: 0 5px;
    }
  }
}

main {
  max-width: 1100px;
  margin: auto;
  align-items: flex-start;
  margin-top: -80px;
  position: relative;
  z-index: 1;
  padding: 0 10px;
  .list {
    max-width: 800px;
    min-height: 80vh;
    background: #fff;
    border-radius: 6px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.07);
    padding: 20px 10px;
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    justify-content: center;
    margin: auto;
    .item {
      width: 220px;
      height: 120px;
      border-radius: 6px;
      background: #f5f5f5;
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
      overflow: hidden;
      background-position: center center;
      background-size: cover;
      background-repeat: no-repeat;
      margin: 5px;
      p {
        color: #fff;
        position: relative;
        z-index: 2;
        text-align: center;
        &:nth-child(1) {
          font-weight: 600;
          @include main-text-hidden(1);
        }
        &:nth-child(2) {
          font-size: 14px;
          margin-top: 2px;
          opacity: 0.8;
        }
      }
      &::before {
        content: "";
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background-color: rgba(0, 0, 0, 0.5);
      }
    }
  }
}
</style>
